package com.teacher.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.teacher.pojo.entity.PartySchool;
import com.teacher.pojo.entity.SummerVolunteer;
import com.teacher.pojo.excel.PartySchoolExcel;
import com.teacher.pojo.excel.SummerVolunteerExcel;
import com.teacher.service.IPartySchoolService;
import com.teacher.service.IPerStudentService;
import com.teacher.service.ISummerVolunteerService;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zfm
 * @Date: 2021/7/30 16:17
 */
public class ExcelSummerVolunteerListener extends AnalysisEventListener<SummerVolunteerExcel> {
    private Long termId;
    private ISummerVolunteerService iSummerVolunteerService;

    private List<SummerVolunteer> summerVolunteerList = new ArrayList<>();
    private IPerStudentService iPerStudentService;

    public ExcelSummerVolunteerListener(Long termId, ISummerVolunteerService iSummerVolunteerService, IPerStudentService iPerStudentService) {
        this.termId = termId;
        this.iSummerVolunteerService = iSummerVolunteerService;
        this.iPerStudentService = iPerStudentService;
    }

    @Override
    public void invoke(SummerVolunteerExcel summerVolunteerExcel, AnalysisContext analysisContext) {
        if (iPerStudentService.isExit(summerVolunteerExcel.getStudentKey())) {
            SummerVolunteer summerVolunteer = new SummerVolunteer();
            BeanUtils.copyProperties(summerVolunteerExcel, summerVolunteer);
            summerVolunteer.setTermId(termId);
            summerVolunteerList.add(summerVolunteer);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        iSummerVolunteerService.saveBatch(summerVolunteerList);
    }
}
